package com.changcheng.service.Imp;
import com.changcheng.mapper.ProductMapper;
import com.changcheng.pojo.Do.PageInfo;
import com.changcheng.pojo.Product;
import com.changcheng.pojo.User;
import com.changcheng.service.FileService;
import com.changcheng.service.ProductService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    ProductMapper productDao;
    @Autowired
    FileService fileService;

    //查询所有产品
    @Override
    public List<Product> select(PageInfo pageInfo) {
        return productDao.select(pageInfo);
    }

    //查询总记录数
    @Override
    public int selectItemsNumber() {
        return productDao.selectItemsNumber();
    }

    //根据ID删除单个产品
    @Override
    public boolean delete(Integer id) {
        return productDao.delete(id);
    }

    @Override
    public String upload(HttpServletRequest request) {
        HttpSession session=request.getSession();
        MultipartFile file=((MultipartHttpServletRequest) request).getFile("file");
        try{
            //1.上传文件
            String url=fileService.upload(file);
            //2.根据返回的url更新数据库
            if(url!=null){
                session.setAttribute("picture",url);
                return url;
            }else{
                session.setAttribute("picture","error");
                return null;
            }
        }catch(Exception e){
            session.setAttribute("picture","error");
            e.printStackTrace();
            return null;
        }
    }

    //更新产品
    @Override
    public boolean update(Product product,HttpServletRequest request) {
        try{
            HttpSession session=request.getSession();
            //获取当前登录此账号的用户id
            Subject subject= SecurityUtils.getSubject();
            User user=(User)subject.getPrincipal();
            //将修改者的id改为当前登录用户的id
            product.setUpdater_id(user.getId());
            //获取上传图片的url地址(判断session是否过期)
            String picture=null;
            if(session.getAttribute("picture")!=null){
                picture=session.getAttribute("picture").toString();
            }
            if(picture!=null){
                product.setPicture(picture);
            }else{
                //session失效或者用户根本没有上传图片
                product.setPicture("error");
            }
            //使用后将session失效，避免下一次用户若未上传图片使用了session，造成污染
            session.removeAttribute("picture");
            return productDao.update(product);
        }catch(Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean updateStateUsingById(Integer id, Integer state) {
        try{
            return productDao.updateStateUsingById(id,state);
        }catch(Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean add(Product product,HttpServletRequest request) {
        try{
            HttpSession session=request.getSession();
            //获取当前登录此账号的用户id
            Subject subject= SecurityUtils.getSubject();
            User user=(User)subject.getPrincipal();
            product.setFounder_id(user.getId());
            //获取上传图片的url地址(判断session是否过期)
            String picture=null;
            if(session.getAttribute("picture")!=null){
                picture=session.getAttribute("picture").toString();
            }
            if(picture!=null){
                product.setPicture(picture);
            }else{
                //session失效或者用户根本没有上传图片
                product.setPicture("error");
            }
            //使用后将session失效，避免下一次用户若未上传图片使用了session，造成污染
            session.removeAttribute("picture");
            return productDao.add(product);
        }catch(Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<Product> customer_search() {
        return productDao.customer_search();
    }
}
